import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class PaintPainter extends CustomPainter {
  late Paint _paint;
  late Path _path;

  PaintPainter() {
    _paint = Paint()
      ..color = Colors.green
      ..strokeWidth = 20
      ..strokeCap = StrokeCap.butt //butt 不出头；round 圆头； square 方头
      //线连接类型
      //StrokeJoin.bevel - 斜角、StrokeJoin.miter - 锐角、StrokeJoin.round - 圆角
      ..style = PaintingStyle.stroke;

    _path = Path();
  }

  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    ///线头处理
    /*canvas.drawLine(Offset(50,50), Offset(50,150), _paint ..strokeCap = StrokeCap.butt);
    canvas.drawLine(Offset(50 + 50,50), Offset(50 + 50,150), _paint ..strokeCap = StrokeCap.round);
    canvas.drawLine(Offset(50 + 100,50), Offset(50 +100,150), _paint ..strokeCap = StrokeCap.square);*/

  /*  ///path划线处理
    _path.moveTo(50, 50);
    _path.lineTo(50, 150);//绝对路径
    _path.relativeLineTo(100, -80);//相对路径，相对于上一个画笔的点
    _path.relativeLineTo(0, 100);
    canvas.drawPath(_path, _paint ..strokeJoin = StrokeJoin.bevel);*/

    drawStrokeMiterLimit(canvas);
  }

  // 测试 strokeMiterLimit 属性
  void drawStrokeMiterLimit(Canvas canvas) {
    Paint paint =  Paint();
    Path path =  Path();
    paint
      ..style = PaintingStyle.stroke
      ..color = Colors.blue
      ..strokeJoin = StrokeJoin.miter
      ..strokeWidth = 20;
    for (int i = 0; i < 4; i++) {
      path.reset();
      path.moveTo(50 + 150.0 * i, 50);
      path.lineTo(50 + 150.0 * i, 150);
      path.relativeLineTo(100, -(40.0 * i + 20));
      canvas.drawPath(path, paint..strokeMiterLimit = 4);
    }

/*    for (int i = 0; i < 4; i++) {
      path.reset();
      path.moveTo(50 + 150.0 * i, 50 + 150.0);
      path.lineTo(50 + 150.0 * i, 150 + 150.0);
      path.relativeLineTo(100, -(40.0 * i + 20));
      canvas.drawPath(
          path,
          paint
            ..strokeMiterLimit = 3);
    }*/
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}
